package csu.web.mypetstore.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;

//退出登录的servlet
@WebServlet(name="SignOffServlet",urlPatterns = {"/logout"})
public class SignOffServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        // 执行注销操作，将 session 中的数据清除
//        req.getSession().invalidate();
        // 清除session
        Enumeration<String> attributeNames = req.getSession().getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String key = attributeNames.nextElement().toString();
            req.getSession().removeAttribute(key);
        }
        // 重定向到首页
        resp.sendRedirect(req.getContextPath() + "/mainForm");
    }
}
